package com.example.demo.common;


import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class demo2 {

    @RequestMapping (value = "/exportt",method = RequestMethod.POST,consumes = "multipart/form-data")
        public Map<String,List<Integer>> exportt(@RequestParam("file") MultipartFile file){
        //System.out.println(file.toString());
        System.out.println("---");
        List<String> a=new ArrayList<>();
        //List<List<Integer>> b=new ArrayList<>();
        Map<String,List<Integer>> b=new HashMap<>();
        List<Integer> jin=new ArrayList<>();
        List<Integer> mu=new ArrayList<>();
        List<Integer> shui=new ArrayList<>();
        List<Integer> huo=new ArrayList<>();
        List<Integer> tu=new ArrayList<>();
       // Map<String,Integer> b=new HashMap<>();
        int index=0;
        InputStream inputStream = null;
        String s="";
        try {
            inputStream = file.getInputStream();
            Workbook workbook = Workbook.getWorkbook(inputStream);
            List<String> strings=new ArrayList();
            Sheet sheet = workbook.getSheet(0);
            int rows = sheet.getRows();
            int columns = sheet.getColumns();
            for (int i = 0; i < rows; i++) {
                Cell cell = sheet.getCell(index, i);
               // System.out.println(cell.getContents());
                String contents = cell.getContents();
                strings.add(contents);
            }
            String[] data = {"金","木","水","火","土"};

            for(int i=0;i<data.length;i++){
                List<Integer> nums = new ArrayList<>();
                for(int j=0;j<strings.size();j++){
                    if(data[i].equals(strings.get(j))){
                       if(strings.get(j).equals("金")) {
                           jin.add(j + 1);
                           b.put("金",jin);
                       }else if(strings.get(j).equals("木")){
                           mu.add(j+1);
                           b.put("木",mu);
                       }else if(strings.get(j).equals("水")){
                           shui.add(j+1);
                           b.put("水",shui);
                       }else if(strings.get(j).equals("火")){
                           huo.add(j+1);
                           b.put("火",huo);
                       }else if(strings.get(j).equals("土")){

                           tu.add(j+1);
                           b.put("土",tu);
                       }

                    }

                }
//                for(int k=0;k<nums.size()-1;k++){
//                    int k1 = k+1;
//                   // System.out.println("第"+k1+"个"+data[i]+"与第"+(k1+1)+"个"+data[i]+"相差"+(nums.get(k1)-nums.get(k)));
//                   // s="第"+k1+"个"+data[i]+"与第"+(k1+1)+"个"+data[i]+"相差"+(nums.get(k1)-nums.get(k)+";");
//                    a.add(s);
//                    //String jin="金："+data[i];
//                   // System.out.println(s);
//                }
            }

        } catch (IOException | BiffException e) {
            e.printStackTrace();
        }
       return  b;
    }
}
